import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;


public class MDChecker {
	
	private ArrayList<String> allowedPasswords;
	
	public MDChecker(){
		allowedPasswords = new ArrayList<String>();
		//adding a random password and hashing it:
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			String msgDigest = "";
			String pass = "password";
			md5.update(pass.getBytes());
			BigInteger hash = new BigInteger(1,md5.digest());
			msgDigest = hash.toString(16);
			allowedPasswords.add(msgDigest);
			
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public boolean checkPassword(String passToCheck){
		boolean result = false;
		Iterator<String> it = allowedPasswords.iterator();
		while(it.hasNext()){
			String crt = it.next();
			if(crt.equals(passToCheck)){
				result = true;
				break;
			}
		}
		return result;
	}
	public void addPassword(String clearPass){
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(clearPass.getBytes());
			BigInteger hash = new BigInteger(1,md5.digest());
			String newDigest = hash.toString(16); 
			allowedPasswords.add(newDigest);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
